import os
import cv2
import utils

SrcDir = r"I:\FirstLabel\8"

for root, dirs, files in os.walk(SrcDir):
    classes = open(root + "\\classes.txt").read().splitlines()
    for file in files:
        if file[-1] == 't':
            print("converting %s..." % file)
            txt = open(SrcDir + "\\" + file).read().splitlines()
            a = SrcDir + "\\" + file[:-3] + "jpg"
            img = cv2.imread(SrcDir + "\\" + file[:-3] + "jpg")
            if img is None:
                print("img is None")
                continue
            NewLocations = [SrcDir + "\\" + file[:-3] + "xml"]
            height, width, _ = img.shape
            for line in txt:
                spt = line.split(" ")
                center_x = round(float(spt[1].strip()) * width)
                center_y = round(float(spt[2].strip()) * height)
                bbox_width = round(float(spt[3].strip()) * width)
                bbox_height = round(float(spt[4].strip()) * height)
                NewLocation = [int(center_x - bbox_width / 2), int(center_y - bbox_height / 2), int(center_x + bbox_width / 2), int(center_y + bbox_height / 2), classes[int(spt[0])]]
                NewLocations.append(NewLocation)
            utils.WriteXml(NewLocations, width, height)
            # os.remove(SrcDir + "\\" + file)
print("--------------------------------done---------------------------")
